﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;

namespace FasterWPF
{
    public static class ExpanderExt
    {     
        public enum Shape
        { 
           Ellipse = 1,
           Rectangle = 2
        }

        public static void ChangeExpanderSkin(this Expander expa, Brush expanderBackground, Brush headerLabelBackground, string circleColor, string arrowColor, string mouseOverCircleColor, string mouseOverArrowColor, string pressedCircleColor, string pressedArrowColor, string headerBackgroundColor, FasterWPF.ExpanderExt.Shape es)
        {
            ExpanderExt.ApplyExpanderStrategy(expa, circleColor, arrowColor, mouseOverCircleColor, mouseOverArrowColor, pressedCircleColor, pressedArrowColor, headerBackgroundColor, es);
            expa.Background = expanderBackground;
            (expa.Header as Label).Background = headerLabelBackground;
        }

        public static void ChangeExpanderSkin(this Expander expa, Brush expanderBackground, Brush headerLabelBackground, string circleColor, string arrowColor, string mouseOverCircleColor, string mouseOverArrowColor, string pressedCircleColor, string pressedArrowColor, string headerBackgroundColor, string expandedArrowGeometryPath, string collapsedArrowGeometryPath, FasterWPF.ExpanderExt.Shape es)
        {
            expa.Template = ExpanderExt.ApplyExpanderStrategyFull(circleColor, arrowColor, mouseOverCircleColor, mouseOverArrowColor, pressedCircleColor, pressedArrowColor, headerBackgroundColor, expandedArrowGeometryPath, collapsedArrowGeometryPath, es);
            expa.Background = Brushes.LightSalmon;
            (expa.Header as Label).Background = Brushes.LightSalmon;
        }

    //    public static void ApplyExpanderStrategyAlternate(this Expander expa, string circleColor, string arrowColor, string mouseOverCircleColor, string mouseOverArrowColor, string pressedCircleColor, string pressedArrowColor, string headerBackgroundColor, Shape es)
    //    {
    //        //Alternate way of accessing ToggleButton inside of Expander
    //        ToggleButton tgb =
    //VisualTreeHelper.GetChild(
    //    VisualTreeHelper.GetChild(
    //        VisualTreeHelper.GetChild(
    //            expa,
    //            0),
    //        0),
    //    0) as ToggleButton;

    //        string xaml = "<ControlTemplate TargetType=\"{x:Type ToggleButton}\"><Border Padding=\"{TemplateBinding Padding}\"><Grid Background=\"" + headerBackgroundColor + "\" SnapsToDevicePixels=\"False\"><Grid.ColumnDefinitions><ColumnDefinition Width=\"19\"/><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><" + ((es == Shape.Ellipse) ? "Ellipse" : "Rectangle") + " x:Name=\"circle\" Width=\"19\" Height=\"19\" Stroke=\"" + circleColor + "\" HorizontalAlignment=\"Center\"   VerticalAlignment=\"Center\"/><Path x:Name=\"arrow\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" Stroke=\"" + arrowColor + "\"  StrokeThickness=\"2\" SnapsToDevicePixels=\"false\" Data=\"M 1,1.5 L 4.5,5 L 8,1.5\"/><ContentPresenter Grid.Column=\"1\" Margin=\"4,0,0,0\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Center\" SnapsToDevicePixels=\"True\" RecognizesAccessKey=\"True\"/></Grid></Border><ControlTemplate.Triggers><Trigger Property=\"IsChecked\" Value=\"true\"><Setter Property=\"Data\" Value=\"M 1,4.5  L 4.5,1  L 8,4.5\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsMouseOver\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + mouseOverCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + mouseOverArrowColor + "\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsPressed\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + pressedCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"StrokeThickness\" Value=\"1.5\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + pressedArrowColor + "\" TargetName=\"arrow\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

    //        MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
    //        ParserContext pc = new ParserContext();
    //        pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
    //        pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
    //        var repBtnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);
    //        tgb.Template = repBtnTemplate;
    //    }

        public static ControlTemplate ApplyExpanderStrategyFull(string circleColor, string arrowColor, string mouseOverCircleColor, string mouseOverArrowColor, string pressedCircleColor, string pressedArrowColor, string headerBackgroundColor, Shape es)
        {
            string xaml = "<ControlTemplate TargetType=\"{x:Type Expander}\"><ControlTemplate.Resources><Style x:Key=\"ExpanderHeaderFocusVisual\"><Setter Property=\"Control.Template\"><Setter.Value><ControlTemplate><Border><Rectangle Margin=\"0\" StrokeThickness=\"1\" Stroke=\"Black\" StrokeDashArray=\"1 2\" SnapsToDevicePixels=\"true\"/></Border></ControlTemplate></Setter.Value></Setter></Style><Style x:Key=\"ExpanderCustomStyle\" TargetType=\"{x:Type ToggleButton}\"><Setter Property=\"Template\"><Setter.Value><ControlTemplate TargetType=\"{x:Type ToggleButton}\"><Border Padding=\"{TemplateBinding Padding}\"><Grid Background=\"" + headerBackgroundColor + "\" SnapsToDevicePixels=\"False\"><Grid.ColumnDefinitions><ColumnDefinition Width=\"19\"/><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><" + ((es == Shape.Ellipse) ? "Ellipse" : "Rectangle") + " x:Name=\"circle\" Width=\"19\" Height=\"19\" Stroke=\"" + circleColor + "\" HorizontalAlignment=\"Center\"   VerticalAlignment=\"Center\"/><Path x:Name=\"arrow\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" Stroke=\"" + arrowColor + "\"  StrokeThickness=\"2\" SnapsToDevicePixels=\"false\" Data=\"M 1,1.5 L 4.5,5 L 8,1.5\"/><ContentPresenter Grid.Column=\"1\" Margin=\"4,0,0,0\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Center\" SnapsToDevicePixels=\"True\" RecognizesAccessKey=\"True\"/></Grid></Border><ControlTemplate.Triggers><Trigger Property=\"IsChecked\" Value=\"true\"><Setter Property=\"Data\" Value=\"M 1,4.5  L 4.5,1  L 8,4.5\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsMouseOver\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + mouseOverCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + mouseOverArrowColor + "\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsPressed\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + pressedCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"StrokeThickness\" Value=\"1.5\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + pressedArrowColor + "\" TargetName=\"arrow\"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></ControlTemplate.Resources><Border BorderBrush=\"{TemplateBinding BorderBrush}\" BorderThickness=\"{TemplateBinding BorderThickness}\" Background=\"{TemplateBinding Background}\" CornerRadius=\"3\" SnapsToDevicePixels=\"true\"><DockPanel><ToggleButton x:Name=\"HeaderSite\" DockPanel.Dock=\"Top\" Margin=\"1\" MinWidth=\"0\" MinHeight=\"0\" Style=\"{StaticResource ExpanderCustomStyle}\" FocusVisualStyle=\"{StaticResource ExpanderHeaderFocusVisual}\" IsChecked=\"{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}\" Content=\"{TemplateBinding Header}\" ContentTemplate=\"{TemplateBinding HeaderTemplate}\" ContentTemplateSelector=\"{TemplateBinding HeaderTemplateSelector}\" Foreground=\"{TemplateBinding Foreground}\" Padding=\"{TemplateBinding Padding}\" FontFamily=\"{TemplateBinding FontFamily}\" FontSize=\"{TemplateBinding FontSize}\" FontStyle=\"{TemplateBinding FontStyle}\" FontStretch=\"{TemplateBinding FontStretch}\" FontWeight=\"{TemplateBinding FontWeight}\" HorizontalContentAlignment=\"{TemplateBinding HorizontalContentAlignment}\" VerticalContentAlignment=\"{TemplateBinding VerticalContentAlignment}\"/><ContentPresenter x:Name=\"ExpandSite\" DockPanel.Dock=\"Bottom\" Visibility=\"Collapsed\" Focusable=\"false\" HorizontalAlignment=\"{TemplateBinding HorizontalContentAlignment}\" VerticalAlignment=\"{TemplateBinding VerticalContentAlignment}\" Margin=\"{TemplateBinding Padding}\"/></DockPanel></Border><ControlTemplate.Triggers><Trigger Property=\"IsExpanded\" Value=\"true\"><Setter Property=\"Visibility\"  Value=\"Visible\" TargetName=\"ExpandSite\"/></Trigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\"/></Trigger><Trigger Property=\"IsExpanded\" Value=\"true\"><Setter Property=\"Visibility\"  Value=\"Visible\" TargetName=\"ExpandSite\"/></Trigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            pc.XmlnsDictionary.Add("theme", "clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero");

            var repBtnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);
            return repBtnTemplate;
        }

        public static ControlTemplate ApplyExpanderStrategyFull(string circleColor, string arrowColor, string mouseOverCircleColor, string mouseOverArrowColor, string pressedCircleColor, string pressedArrowColor, string headerBackgroundColor, string expandedArrowGeometryPath, string collapsedArrowGeometryPath, Shape es)
        {
            string xaml = "<ControlTemplate TargetType=\"{x:Type Expander}\"><ControlTemplate.Resources><Style x:Key=\"ExpanderHeaderFocusVisual\"><Setter Property=\"Control.Template\"><Setter.Value><ControlTemplate><Border><Rectangle Margin=\"0\" StrokeThickness=\"1\" Stroke=\"Black\" StrokeDashArray=\"1 2\" SnapsToDevicePixels=\"true\"/></Border></ControlTemplate></Setter.Value></Setter></Style><Style x:Key=\"ExpanderCustomStyle\" TargetType=\"{x:Type ToggleButton}\"><Setter Property=\"Template\"><Setter.Value><ControlTemplate TargetType=\"{x:Type ToggleButton}\"><Border Padding=\"{TemplateBinding Padding}\"><Grid Background=\"" + headerBackgroundColor + "\" SnapsToDevicePixels=\"False\"><Grid.ColumnDefinitions><ColumnDefinition Width=\"19\"/><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><" + ((es == Shape.Ellipse) ? "Ellipse" : "Rectangle") + " x:Name=\"circle\" Width=\"19\" Height=\"19\" Stroke=\"" + circleColor + "\" HorizontalAlignment=\"Center\"   VerticalAlignment=\"Center\"/><Path x:Name=\"arrow\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" Stroke=\"" + arrowColor + "\"  StrokeThickness=\"2\" SnapsToDevicePixels=\"false\" Data=\"" + collapsedArrowGeometryPath + "\"/><ContentPresenter Grid.Column=\"1\" Margin=\"4,0,0,0\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Center\" SnapsToDevicePixels=\"True\" RecognizesAccessKey=\"True\"/></Grid></Border><ControlTemplate.Triggers><Trigger Property=\"IsChecked\" Value=\"true\"><Setter Property=\"Data\" Value=\"" + expandedArrowGeometryPath + "\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsMouseOver\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + mouseOverCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + mouseOverArrowColor + "\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsPressed\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + pressedCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"StrokeThickness\" Value=\"1.5\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + pressedArrowColor + "\" TargetName=\"arrow\"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></ControlTemplate.Resources><Border BorderBrush=\"{TemplateBinding BorderBrush}\" BorderThickness=\"{TemplateBinding BorderThickness}\" Background=\"{TemplateBinding Background}\" CornerRadius=\"3\" SnapsToDevicePixels=\"true\"><DockPanel><ToggleButton x:Name=\"HeaderSite\" DockPanel.Dock=\"Top\" Margin=\"1\" MinWidth=\"0\" MinHeight=\"0\" Style=\"{StaticResource ExpanderCustomStyle}\" FocusVisualStyle=\"{StaticResource ExpanderHeaderFocusVisual}\" IsChecked=\"{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}\" Content=\"{TemplateBinding Header}\" ContentTemplate=\"{TemplateBinding HeaderTemplate}\" ContentTemplateSelector=\"{TemplateBinding HeaderTemplateSelector}\" Foreground=\"{TemplateBinding Foreground}\" Padding=\"{TemplateBinding Padding}\" FontFamily=\"{TemplateBinding FontFamily}\" FontSize=\"{TemplateBinding FontSize}\" FontStyle=\"{TemplateBinding FontStyle}\" FontStretch=\"{TemplateBinding FontStretch}\" FontWeight=\"{TemplateBinding FontWeight}\" HorizontalContentAlignment=\"{TemplateBinding HorizontalContentAlignment}\" VerticalContentAlignment=\"{TemplateBinding VerticalContentAlignment}\"/><ContentPresenter x:Name=\"ExpandSite\" DockPanel.Dock=\"Bottom\" Visibility=\"Collapsed\" Focusable=\"false\" HorizontalAlignment=\"{TemplateBinding HorizontalContentAlignment}\" VerticalAlignment=\"{TemplateBinding VerticalContentAlignment}\" Margin=\"{TemplateBinding Padding}\"/></DockPanel></Border><ControlTemplate.Triggers><Trigger Property=\"IsExpanded\" Value=\"true\"><Setter Property=\"Visibility\"  Value=\"Visible\" TargetName=\"ExpandSite\"/></Trigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\"/></Trigger><Trigger Property=\"IsExpanded\" Value=\"true\"><Setter Property=\"Visibility\"  Value=\"Visible\" TargetName=\"ExpandSite\"/></Trigger><Trigger Property=\"IsEnabled\" Value=\"false\"><Setter Property=\"Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            pc.XmlnsDictionary.Add("theme", "clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero");

            var repBtnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);
            return repBtnTemplate;
        }

        public static void ApplyExpanderStrategy(this Expander expa, string circleColor, string arrowColor, string mouseOverCircleColor, string mouseOverArrowColor, string pressedCircleColor, string pressedArrowColor, string headerBackgroundColor, Shape es)
        {
            expa.ApplyTemplate();
            ToggleButton tgb = (ToggleButton)expa.Template.FindName("HeaderSite", expa);

            string xaml = "<ControlTemplate TargetType=\"{x:Type ToggleButton}\"><Border Padding=\"{TemplateBinding Padding}\"><Grid Background=\"" + headerBackgroundColor + "\" SnapsToDevicePixels=\"False\"><Grid.ColumnDefinitions><ColumnDefinition Width=\"19\"/><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><" + ((es == Shape.Ellipse ) ? "Ellipse" : "Rectangle") + " x:Name=\"circle\" Width=\"19\" Height=\"19\" Stroke=\"" + circleColor + "\" HorizontalAlignment=\"Center\"   VerticalAlignment=\"Center\"/><Path x:Name=\"arrow\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" Stroke=\"" + arrowColor + "\"  StrokeThickness=\"2\" SnapsToDevicePixels=\"false\" Data=\"M 1,1.5 L 4.5,5 L 8,1.5\"/><ContentPresenter Grid.Column=\"1\" Margin=\"4,0,0,0\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Center\" SnapsToDevicePixels=\"True\" RecognizesAccessKey=\"True\"/></Grid></Border><ControlTemplate.Triggers><Trigger Property=\"IsChecked\" Value=\"true\"><Setter Property=\"Data\" Value=\"M 1,4.5  L 4.5,1  L 8,4.5\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsMouseOver\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + mouseOverCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + mouseOverArrowColor + "\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsPressed\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + pressedCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"StrokeThickness\" Value=\"1.5\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + pressedArrowColor + "\" TargetName=\"arrow\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            var repBtnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);
            tgb.Template = repBtnTemplate;
        }

        public static void ApplyExpanderStrategy(this Expander expa, string circleColor, string arrowColor, string mouseOverCircleColor, string mouseOverArrowColor, string pressedCircleColor, string pressedArrowColor, string headerBackgroundColor, string expandedArrowGeometryPath, string collapsedArrowGeometryPath, Shape es)
        {
            expa.ApplyTemplate();
            ToggleButton tgb = (ToggleButton)expa.Template.FindName("HeaderSite", expa);

            string xaml = "<ControlTemplate TargetType=\"{x:Type ToggleButton}\"><Border Padding=\"{TemplateBinding Padding}\"><Grid Background=\"" + headerBackgroundColor + "\" SnapsToDevicePixels=\"False\"><Grid.ColumnDefinitions><ColumnDefinition Width=\"19\"/><ColumnDefinition Width=\"*\"/></Grid.ColumnDefinitions><" + ((es == Shape.Ellipse) ? "Ellipse" : "Rectangle") + " x:Name=\"circle\" Width=\"19\" Height=\"19\" Stroke=\"" + circleColor + "\" HorizontalAlignment=\"Center\"   VerticalAlignment=\"Center\"/><Path x:Name=\"arrow\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" Stroke=\"" + arrowColor + "\"  StrokeThickness=\"2\" SnapsToDevicePixels=\"false\" Data=\"" + collapsedArrowGeometryPath + "\"/><ContentPresenter Grid.Column=\"1\" Margin=\"4,0,0,0\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Center\" SnapsToDevicePixels=\"True\" RecognizesAccessKey=\"True\"/></Grid></Border><ControlTemplate.Triggers><Trigger Property=\"IsChecked\" Value=\"true\"><Setter Property=\"Data\" Value=\"" + expandedArrowGeometryPath + "\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsMouseOver\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + mouseOverCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + mouseOverArrowColor + "\" TargetName=\"arrow\"/></Trigger><Trigger Property=\"IsPressed\" Value=\"true\"><Setter Property=\"Stroke\" Value=\"" + pressedCircleColor + "\" TargetName=\"circle\"/><Setter Property=\"StrokeThickness\" Value=\"1.5\" TargetName=\"circle\"/><Setter Property=\"Stroke\" Value=\"" + pressedArrowColor + "\" TargetName=\"arrow\"/></Trigger></ControlTemplate.Triggers></ControlTemplate>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            var repBtnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);
            tgb.Template = repBtnTemplate;
        }

        public static Expander SetBackgroundColor(this Expander chain, Brush brush)
        {
            Brush newBrush = (brush);
            chain.Background = newBrush;
            return chain;
        }
        
        public static Expander Initialize(this Expander source, ContainerType containerType,  int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return Initialize(source, double.NaN, double.NaN, containerType); //Default width and height to Auto by setting to NaN
        }

        public static Expander Initialize(this Expander source, ContainerType containerType, object header, int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return Initialize(source, double.NaN, double.NaN, containerType, header); //Default width and height to Auto by setting to NaN
        }

        public static Expander Initialize(this Expander source, double width, double height, ContainerType containerType, object header)
        {
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Header = header;

            sharedExpanderInitializeCode(source, width, height, containerType);

            return source;
        }

        public static Expander Initialize(this Expander source, double width, double height, ContainerType containerType)
        {   
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            sharedExpanderInitializeCode(source, width, height, containerType);

            return source;
        }

        private static void sharedExpanderInitializeCode(Expander source, double width, double height, ContainerType containerType)
        {
            source.Width = width;
            source.Height = height;

            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<Expander>(source);
            source.SetIComposite<Expander>(icomp);

            //STORE SETTING BUILD CHAIN AND GET CHAIN?!?  Put these in a Composite implements an interface
            ExpanderParent gbsr = new ExpanderParent();
            source.StoreSetting<Expander>("IParent", gbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<Expander>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });
        }
        
    }//end of class
}//end of namespace
